package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.exception.ExceptionEnum;
import com.hamster.crmpre.exception.LyException;
import com.hamster.crmpre.mapper.*;
import com.hamster.crmpre.pojo.*;
import com.hamster.crmpre.service.HandOverService;
import com.hamster.crmpre.service.MessageQueryService;
import com.hamster.crmpre.service.PhoneUserService;
import com.hamster.crmpre.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：Lizhipeng
 * @date ：Created in 2019/12/30 11:46
 * @description：转交接口实现
 * @modified By：
 * @version:
 */
@Service
public class HandOverServiceImpl implements HandOverService {
    @Autowired
    private PhoneUserService phoneUserService;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private HandOverPersonalDevelopmentCustomerMapper handOverPersonalDevelopmentCustomerMapper;
    @Autowired
    private HandOverVipMapper handOverVipMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private CustomerToSuperiorMapper customerToSuperiorMapper;
    @Autowired
    private HandOverAssistantMapper handOverAssistantMapper;
    @Autowired
    private PersonalMessageMapper personalMessageMapper;
    @Autowired
    private PotentialCustomerMapper potentialCustomerMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private MessageQueryService messageQueryService;
    /**
     * 获取转交客户页面的内容
     * @param messageLink
     */
    @Override
    public HandOverPersonalDevelopmentCustomer handOverDevelopCustomerGet(String messageLink) {

        return handOverPersonalDevelopmentCustomerMapper.selectById(messageLink);
    }



//    /**
//     * 转交个人开发客户提交按钮实现方法
//     * @param map
//     * senderId   原始人的id
//     * employeeId  目标人的id
//     * id  详细信息的id
//     */
//    @Override
//    @Transactional
//    public void developCustomerSubmit(Map<String, String> map) {
//        //拿出满足条件的客户列表
//        String senderId = map.get("senderId");//任务发起人id
//        String employeeId = map.get("employeeId");//被转交人id
//        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("developer_id",senderId)
//                .or().eq("principal_course_id",senderId)
//                .or().eq("principal_vip_id",senderId)
//                .or().eq("principal_company_id",senderId);
//        List<Customer> customerList = customerMapper.selectList(queryWrapper);
//        //进行更改【正式客户】
//        for (Customer customer : customerList) {
//            if (customer.getDeveloperId().equals(senderId)){
//                customer.setDeveloperId(employeeId);
//            } else if (customer.getPrincipalCourseId().equals(senderId)) {
//                customer.setPrincipalCourseId(employeeId);
//            } else if (customer.getPrincipalVipId().equals(senderId)) {
//                customer.setPrincipalVipId(employeeId);
//            } else if (customer.getPrincipalCompanyId().equals(senderId)) {
//                customer.setPrincipalCompanyId(employeeId);
//            }
//        }
//        //进行更改【潜在客户】
//        potentialCustomerMapper.updateByUser(senderId,employeeId);
//
//        //改变消息的状态
//        messageQueryService.setRead(map.get("id"));//标记已读
//        //获取要操作的消息详情对象
//        PersonalMessage personalMessage = messageQueryService.get(map.get("id"));
//        String messageLink = personalMessage.getMessageLink();
//
//        HandOverPersonalDevelopmentCustomer handOverPersonalDevelopmentCustomer = handOverPersonalDevelopmentCustomerMapper.selectById(messageLink);
//        handOverPersonalDevelopmentCustomer.setState("1");//已提交
//        handOverPersonalDevelopmentCustomer.setAuditorId(LoginInterceptor.getLoginUser().getUserId());
//        String userName = phoneUserService.queryNameByPhone(LoginInterceptor.getLoginUser().getPhone());
//        handOverPersonalDevelopmentCustomer.setAuditorName(userName);
//        handOverPersonalDevelopmentCustomer.setEmployeeId(map.get("employeeId"));
//        handOverPersonalDevelopmentCustomer.setEmployeeName(map.get("employeeName"));
//        handOverPersonalDevelopmentCustomerMapper.updateById(handOverPersonalDevelopmentCustomer);
//
//
//        //给财务发一条通知
//        //通知财务一声
//        Notice notice = new Notice();
//        Customer customer1 = customerMapper.selectById(senderId);
//        String emp1 = customer1.getName();//发送人
//        Customer customer2 = customerMapper.selectById(employeeId);
//        String emp2 = customer2.getName();//接收人
//
//        String contentMessage = "员工："+emp1+" 转交自己开发的客户给 "+"员工："+emp2;
//        notice.setContent(contentMessage);
//        noticeMapper.insert(notice);
//
//        PersonalMessage personalMessage1 = new PersonalMessage();
//        personalMessage1.setMessageType("通知消息");
//        personalMessage1.setSenderId("系统");
//        personalMessage1.setSenderName("系统");
//        personalMessage1.setMessageLink(notice.getId());
//        personalMessage1.setCreateTime(new Date());
//        personalMessage1.setState("0");//消息状态 0未读
//        personalMessage1.setReceiverId("财务部");
//        personalMessage1.setReceiverName("财务部");
//        personalMessage1.setDepartment("财务部");
//        personalMessageMapper.insert(personalMessage1);
//    }

    /**
     * 转交VIP提交按钮实现方法
     * @param map
     */
    @Override
    @Transactional
    public void vip(Map<String, String> map) {
//        判断该消息，转交的是vip还是小分公司，大分公司

        //获取该VIP客户对象
        Customer vipCustomer = customerMapper.selectById(map.get("customerId"));
        //获取该客户类型
        String messageType = map.get("messageType");
        //为该客户进行员工分配
        if ("转交VIP".equals(messageType)){
            vipCustomer.setPrincipalVipId(map.get("employeeId"));
        }else if ("转交小分公司".equals(messageType) || "转交大分公司".equals(messageType)){
            vipCustomer.setPrincipalCompanyId(map.get("employeeId"));
        }

        //更新客户资料
        customerMapper.updateById(vipCustomer);

        //改变该条消息的状态
        messageQueryService.setRead(map.get("id"));//标记消息已读
        PersonalMessage personalMessage = messageQueryService.get(map.get("id"));

        //获取要操作的对象的详细消息的对象的id
        String messageLink = personalMessage.getMessageLink();
        HandOverVip handOverVip = handOverVipMapper.selectById(messageLink);
        handOverVip.setState("1");
        handOverVip.setAuditorId(LoginInterceptor.getLoginUser().getUserId());
//        handOverVip.setAuditorName(LoginInterceptor.getLoginUser().getName());
        String userName = phoneUserService.queryNameByPhone(LoginInterceptor.getLoginUser().getPhone());
        handOverVip.setAuditorName(userName);
        handOverVip.setVipCustomerName(map.get("customerName"));
        handOverVip.setVipCustomerId(map.get("customerId"));
        //更新消息详情表
        handOverVipMapper.updateById(handOverVip);
    }

    /**
     * 转交个人开发客户给上级消息创建【暂时不使用】
     */
//    @Override
//    @Transactional
//    public void customerToSuperior() {
//        //查找出上级领导id
//        String userId = LoginInterceptor.getLoginUser().getUserId();
//        String userName = phoneUserService.queryNameByPhone(LoginInterceptor.getLoginUser().getPhone());
////        String userName = LoginInterceptor.getLoginUser().getName();
//        Employee employeeId = employeeMapper.selectById(userId);
//        Employee superior = employeeMapper.selectById(employeeId.getSuperiorId());
//
//        //判断上级领导是否是经理或者总监
//        if (superior.getPosition().equals("2")||superior.getPosition().equals("3")) {
//            //创建消息详情
//            CustomerToSuperior customerToSuperior = new CustomerToSuperior();
//            customerToSuperior.setEmployeeId(userId);
//            customerToSuperior.setEmployeeName(userName);
//            customerToSuperior.setSuperiorId(superior.getId());
//            customerToSuperior.setSuperiorName(superior.getName());
//            customerToSuperior.setInputUser(userId);
//            customerToSuperior.setInputTime(new Date());
//            customerToSuperior.setDel("0");
//
//            //录入进数据库
//            customerToSuperiorMapper.insert(customerToSuperior);
//
//            //创建个人消息中心的消息
//            PersonalMessage personalMessage = new PersonalMessage();
//            personalMessage.setReceiverId(superior.getId());
//            personalMessage.setReceiverName(superior.getName());
//            personalMessage.setMessageType("转交个人开发客户");
//            personalMessage.setSenderId(userId);
//            personalMessage.setSenderName(userName);
//
//            //拿出数据库自动生成的id作为个人消息中心messageLink
//            personalMessage.setMessageLink(customerToSuperior.getId());
//            personalMessage.setDepartment("开发部");
//            personalMessage.setState("0");
//            personalMessage.setCreateTime(new Date());
//            personalMessage.setInputUser(userId);
//            personalMessage.setDel("0");
//
//            //录入数据库
//            personalMessageMapper.insert(personalMessage);
//
//        }else {//如果不是，那就获得上级的上级，这次肯定是，如果还不是数据就有问题
//            Employee superior2 = employeeMapper.selectById(superior.getSuperiorId());
//            //判断上级领导是否是经理或者总监
//            if (superior2.getPosition().equals("2")||superior2.getPosition().equals("3")) {
//                //创建消息详情
//                CustomerToSuperior customerToSuperior = new CustomerToSuperior();
//                customerToSuperior.setEmployeeId(userId);
//                customerToSuperior.setEmployeeName(userName);
//                customerToSuperior.setSuperiorId(superior2.getId());
//                customerToSuperior.setSuperiorName(superior2.getName());
//                customerToSuperior.setInputUser(userId);
//                customerToSuperior.setInputTime(new Date());
//                customerToSuperior.setDel("0");
//
//                //录入进数据库
//                customerToSuperiorMapper.insert(customerToSuperior);
//
//                //创建个人消息中心的消息
//                PersonalMessage personalMessage = new PersonalMessage();
//                personalMessage.setReceiverId(superior2.getId());
//                personalMessage.setReceiverName(superior2.getName());
//                personalMessage.setMessageType("转交个人开发客户");
//                personalMessage.setSenderId(userId);
//                personalMessage.setSenderName(userName);
//
//                //拿出数据库自动生成的id作为个人消息中心messageLink
//                personalMessage.setMessageLink(customerToSuperior.getId());
//                personalMessage.setDepartment("开发部");
//                personalMessage.setState("0");
//                personalMessage.setCreateTime(new Date());
//                personalMessage.setInputUser(userId);
//                personalMessage.setDel("0");
//
//                //录入数据库
//                personalMessageMapper.insert(personalMessage);
//
//            }
//
//        }
//    }

    /**
     * 转交助教消息创建
     */
    @Override
    @Transactional
    public void assistant(Map<String, String> map) {
        //获取前台传进来的被转交人(消息接收者)和备注
        String receiverId = map.get("id");
        String receiverName = map.get("name");
        String remark = map.get("remark");
        //要被转交的客户的id
        String customerId = map.get("customerId");
        //获取当前登陆者id和姓名
        String userId = LoginInterceptor.getLoginUser().getUserId();
        String userName = phoneUserService.queryNameByPhone(LoginInterceptor.getLoginUser().getPhone());
//        String userName = LoginInterceptor.getLoginUser().getName();

//        Employee employee = employeeMapper.selectById(userId);
        Customer customer = customerMapper.selectById(customerId);
        //String serialize = JsonUtils.serialize(customer);//对象转成字符串
        //创建转交助教详情对象
        HandOverAssistant handOverAssistant = new HandOverAssistant();
        handOverAssistant.setCustomerId(customerId);
        handOverAssistant.setCustomer(customer.getName());
        handOverAssistant.setSender(userId);
        handOverAssistant.setReceiveFlag("0");
        handOverAssistant.setRemark(remark);
        handOverAssistant.setInputUser(userId);
        handOverAssistant.setInputTime(new Date());
        handOverAssistant.setDel("0");

        //存入数据库
        handOverAssistantMapper.insert(handOverAssistant);

        //创建个人消息中心的消息
        PersonalMessage personalMessage = new PersonalMessage();
        personalMessage.setReceiverId(receiverId);
        personalMessage.setReceiverName(receiverName);
        personalMessage.setMessageType("转交助教");
        personalMessage.setSenderId(userId);
        personalMessage.setSenderName(userName);

        //拿出数据库自动生成的id作为个人消息中心messageLink
        personalMessage.setMessageLink(handOverAssistant.getId());
       // personalMessage.setDepartment("财务部");
        personalMessage.setState("0");
        personalMessage.setCreateTime(new Date());
        personalMessage.setInputUser(userId);
        personalMessage.setDel("0");

        //存入数据库
        personalMessageMapper.insert(personalMessage);

    }

    @Override
    public HandOverVip getVip(String id) {
        return handOverVipMapper.selectById(id);
    }

    /**
     * 开发部移交客户  这里发起 移交客户 消息
     * @param employeeId    员工id
     * @param customerId    客户id
     */
    @Override
    @Transactional
    public void divertCustomer(String employeeId, String customerId) {
        Employee employee = employeeMapper.selectById(employeeId);
        Customer customer = customerMapper.selectById(customerId);

        //如果已经提交了“移交客户”申请，但未审核，则抛异常提示【根据发起人，和 转交客户 判断】
        QueryWrapper<HandOverPersonalDevelopmentCustomer> handOverPersonalDevelopmentCustomerQueryWrapper = new QueryWrapper<>();
        handOverPersonalDevelopmentCustomerQueryWrapper.eq("old_employee_id",employeeId).eq("customer_id",customerId).eq("state","0");
        Integer count = handOverPersonalDevelopmentCustomerMapper.selectCount(handOverPersonalDevelopmentCustomerQueryWrapper);
        if (count > 0){
            throw new LyException(ExceptionEnum.ALREADY_SUBMITTED);
        }

        //判断当前员工的身份
        //1. 如果是总监就是自己审核
        //2. 如果是经理就交给上级总监审核
        //3. 如果是主管或者员工，就交给他们的上级 经理审核。

        //0 员工  1 主管  2 经理  3 总监

        //员工【获取他上级的上级，肯定是经理】如果没有上级，则抛异常
        if ("0".equals(employee.getPosition())){
            Employee superior1 = employeeMapper.selectById(employee.getSuperiorId());
            if (superior1 == null){
                throw new LyException(ExceptionEnum.SUPERIOR_IS_NULL);
            }
            Employee superior2 = employeeMapper.selectById(superior1.getSuperiorId());
            if (superior2 == null){
                throw new LyException(ExceptionEnum.SUPERIOR_SUPERIOR_IS_NULL);
            }
            //创建消息详情
            HandOverPersonalDevelopmentCustomer handOverPersonalDevelopmentCustomer = new HandOverPersonalDevelopmentCustomer();
            handOverPersonalDevelopmentCustomer.setDel("0");
            handOverPersonalDevelopmentCustomer.setCustomerId(customer.getId());//客户id
            handOverPersonalDevelopmentCustomer.setCustomerName(customer.getName());//客户姓名
            handOverPersonalDevelopmentCustomer.setOldEmployeeId(employeeId);//发起人员工 id
            handOverPersonalDevelopmentCustomer.setOldEmployeeName(employee.getName());// 发起人员工 姓名
            handOverPersonalDevelopmentCustomer.setManagerId(superior2.getId());//设置上级领导id
            handOverPersonalDevelopmentCustomer.setManagerName(superior2.getName());//设置上级领导姓名
            handOverPersonalDevelopmentCustomer.setInputTime(new Date());
            handOverPersonalDevelopmentCustomer.setInputUser(employeeId);
            handOverPersonalDevelopmentCustomer.setState("0"); //消息状态   0未分配   1已分配

            //录入进数据库
            handOverPersonalDevelopmentCustomerMapper.insert(handOverPersonalDevelopmentCustomer);

            //创建个人消息中心的消息
            PersonalMessage personalMessage = new PersonalMessage();
            personalMessage.setReceiverId(superior2.getId());
            personalMessage.setReceiverName(superior2.getName());
            personalMessage.setMessageType("移交客户");
            personalMessage.setSenderId(employeeId);
            personalMessage.setSenderName(employee.getName());

            //拿出数据库自动生成的id作为个人消息中心messageLink
            personalMessage.setMessageLink(handOverPersonalDevelopmentCustomer.getId());
            personalMessage.setState("0");
            personalMessage.setCreateTime(new Date());
            personalMessage.setInputUser(employeeId);
            personalMessage.setDel("0");

            //录入数据库
            personalMessageMapper.insert(personalMessage);
        }

        //主管【获取他的上级，肯定是经理】如果没有上级，则抛异常
        if ("1".equals(employee.getPosition())){
            Employee superior1 = employeeMapper.selectById(employee.getSuperiorId());
            if (superior1 == null){
                throw new LyException(ExceptionEnum.SUPERIOR_IS_NULL);
            }
            //创建消息详情
            HandOverPersonalDevelopmentCustomer handOverPersonalDevelopmentCustomer = new HandOverPersonalDevelopmentCustomer();
            handOverPersonalDevelopmentCustomer.setDel("0");
            handOverPersonalDevelopmentCustomer.setCustomerId(customer.getId());//客户id
            handOverPersonalDevelopmentCustomer.setCustomerName(customer.getName());//客户姓名
            handOverPersonalDevelopmentCustomer.setOldEmployeeId(employeeId);//发起人员工 id
            handOverPersonalDevelopmentCustomer.setOldEmployeeName(employee.getName());// 发起人员工 姓名
            handOverPersonalDevelopmentCustomer.setManagerId(superior1.getId());//设置上级领导id
            handOverPersonalDevelopmentCustomer.setManagerName(superior1.getName());//设置上级领导姓名
            handOverPersonalDevelopmentCustomer.setInputTime(new Date());
            handOverPersonalDevelopmentCustomer.setInputUser(employeeId);
            handOverPersonalDevelopmentCustomer.setState("0"); //消息状态   0未分配   1已分配

            //录入进数据库
            handOverPersonalDevelopmentCustomerMapper.insert(handOverPersonalDevelopmentCustomer);

            //创建个人消息中心的消息
            PersonalMessage personalMessage = new PersonalMessage();
            personalMessage.setReceiverId(superior1.getId());
            personalMessage.setReceiverName(superior1.getName());
            personalMessage.setMessageType("移交客户");
            personalMessage.setSenderId(employeeId);
            personalMessage.setSenderName(employee.getName());

            //拿出数据库自动生成的id作为个人消息中心messageLink
            personalMessage.setMessageLink(handOverPersonalDevelopmentCustomer.getId());
            personalMessage.setState("0");
            personalMessage.setCreateTime(new Date());
            personalMessage.setInputUser(employeeId);
            personalMessage.setDel("0");

            //录入数据库
            personalMessageMapper.insert(personalMessage);

        }
        //经理【获取他的上级，肯定是总监】如果没有上级，则抛异常
        if ("2".equals(employee.getPosition())){
            Employee superior1 = employeeMapper.selectById(employee.getSuperiorId());
            if (superior1 == null){
                throw new LyException(ExceptionEnum.SUPERIOR_IS_NULL);
            }
            //创建消息详情
            HandOverPersonalDevelopmentCustomer handOverPersonalDevelopmentCustomer = new HandOverPersonalDevelopmentCustomer();
            handOverPersonalDevelopmentCustomer.setDel("0");
            handOverPersonalDevelopmentCustomer.setCustomerId(customer.getId());//客户id
            handOverPersonalDevelopmentCustomer.setCustomerName(customer.getName());//客户姓名
            handOverPersonalDevelopmentCustomer.setOldEmployeeId(employeeId);//发起人员工 id
            handOverPersonalDevelopmentCustomer.setOldEmployeeName(employee.getName());// 发起人员工 姓名
            handOverPersonalDevelopmentCustomer.setManagerId(superior1.getId());//设置上级领导id
            handOverPersonalDevelopmentCustomer.setManagerName(superior1.getName());//设置上级领导姓名
            handOverPersonalDevelopmentCustomer.setInputTime(new Date());
            handOverPersonalDevelopmentCustomer.setInputUser(employeeId);
            handOverPersonalDevelopmentCustomer.setState("0"); //消息状态   0未分配   1已分配

            //录入进数据库
            handOverPersonalDevelopmentCustomerMapper.insert(handOverPersonalDevelopmentCustomer);

            //创建个人消息中心的消息
            PersonalMessage personalMessage = new PersonalMessage();
            personalMessage.setReceiverId(superior1.getId());
            personalMessage.setReceiverName(superior1.getName());
            personalMessage.setMessageType("移交客户");
            personalMessage.setSenderId(employeeId);
            personalMessage.setSenderName(employee.getName());

            //拿出数据库自动生成的id作为个人消息中心messageLink
            personalMessage.setMessageLink(handOverPersonalDevelopmentCustomer.getId());
            personalMessage.setState("0");
            personalMessage.setCreateTime(new Date());
            personalMessage.setInputUser(employeeId);
            personalMessage.setDel("0");

            //录入数据库
            personalMessageMapper.insert(personalMessage);
        }
        //总监
        if ("3".equals(employee.getPosition())){

            //创建消息详情
            HandOverPersonalDevelopmentCustomer handOverPersonalDevelopmentCustomer = new HandOverPersonalDevelopmentCustomer();
            handOverPersonalDevelopmentCustomer.setDel("0");
            handOverPersonalDevelopmentCustomer.setCustomerId(customer.getId());//客户id
            handOverPersonalDevelopmentCustomer.setCustomerName(customer.getName());//客户姓名
            handOverPersonalDevelopmentCustomer.setOldEmployeeId(employeeId);//发起人员工 id
            handOverPersonalDevelopmentCustomer.setOldEmployeeName(employee.getName());// 发起人员工 姓名
            handOverPersonalDevelopmentCustomer.setManagerId(employeeId);//设置上级领导id
            handOverPersonalDevelopmentCustomer.setManagerName(employee.getName());//设置上级领导姓名
            handOverPersonalDevelopmentCustomer.setInputTime(new Date());
            handOverPersonalDevelopmentCustomer.setInputUser(employeeId);
            handOverPersonalDevelopmentCustomer.setState("0"); //消息状态   0未分配   1已分配

            //录入进数据库
            handOverPersonalDevelopmentCustomerMapper.insert(handOverPersonalDevelopmentCustomer);


            //创建个人消息中心的消息
            PersonalMessage personalMessage = new PersonalMessage();
            personalMessage.setReceiverId(employeeId);
            personalMessage.setReceiverName(employee.getName());
            personalMessage.setMessageType("移交客户");
            personalMessage.setSenderId(employeeId);
            personalMessage.setSenderName(employee.getName());

            //拿出消息详情表自动生成的id作为个人消息中心messageLink
            personalMessage.setMessageLink(handOverPersonalDevelopmentCustomer.getId());
            personalMessage.setState("0");
            personalMessage.setCreateTime(new Date());
            personalMessage.setInputUser(employeeId);
            personalMessage.setDel("0");

            //录入数据库
            personalMessageMapper.insert(personalMessage);
        }

    }

    /**
     * 领导审核 移交客户
     * @param map
     */
    @Override
    @Transactional
    public void divertCustomerSubmit(Map<String, String> map) {
        String userId = LoginInterceptor.getLoginUser().getUserId();
        Employee employee = employeeMapper.selectById(userId);
        String employeeId = map.get("employeeId");//新负责人的id
        String employeeName = map.get("employeeName");//新负责人的姓名
        String id = map.get("id");
        //1.更新消息
        messageQueryService.setRead(id);//设置已读
        PersonalMessage personalMessage = messageQueryService.get(id);
        String messageLink = personalMessage.getMessageLink();
        HandOverPersonalDevelopmentCustomer handOverPersonalDevelopmentCustomer = handOverPersonalDevelopmentCustomerMapper.selectById(messageLink);
        handOverPersonalDevelopmentCustomer.setState("1");//消息状态   0未分配   1已分配
        handOverPersonalDevelopmentCustomer.setInputUser(employee.getId());
        handOverPersonalDevelopmentCustomer.setInputTime(new Date());
        handOverPersonalDevelopmentCustomer.setAuditorId(employee.getId());//审批人id
        handOverPersonalDevelopmentCustomer.setAuditorName(employee.getName());//审批人姓名
        handOverPersonalDevelopmentCustomer.setEmployeeId(employeeId);
        handOverPersonalDevelopmentCustomer.setEmployeeName(employeeName);
        //更新消息详情记录
        handOverPersonalDevelopmentCustomerMapper.updateById(handOverPersonalDevelopmentCustomer);

        //2.执行操作
        Customer customer = customerMapper.selectById(handOverPersonalDevelopmentCustomer.getCustomerId());
        //判断是否更换课程负责人
        if (handOverPersonalDevelopmentCustomer.getOldEmployeeId().equals(customer.getPrincipalCourseId())) {
            customer.setPrincipalCourseId(employeeId);
        }
        //判断是否更换vip负责人
        if (handOverPersonalDevelopmentCustomer.getOldEmployeeId().equals(customer.getPrincipalVipId())) {
            customer.setPrincipalVipId(employeeId);
        }
        //判断是否更换分公司负责人
        if (handOverPersonalDevelopmentCustomer.getOldEmployeeId().equals(customer.getPrincipalCompanyId())) {
            customer.setPrincipalCompanyId(employeeId);
        }
        //更新记录
        customerMapper.updateById(customer);

        //3.给申请人发送回执消息通知【给原始负责员工发送回执消息】
        Notice notice = new Notice();
        notice.setContent("您的原客户："+handOverPersonalDevelopmentCustomer.getCustomerName()+",已经成功移交给： "+handOverPersonalDevelopmentCustomer.getEmployeeName());
        noticeMapper.insert(notice);

        PersonalMessage personalMessage1 = new PersonalMessage();
        personalMessage1.setMessageType("通知消息");
        personalMessage1.setSenderId("系统");
        personalMessage1.setSenderName("系统");
        personalMessage1.setMessageLink(notice.getId());
        personalMessage1.setCreateTime(new Date());
        personalMessage1.setState("0");//消息状态 0未读
        personalMessage1.setReceiverId(handOverPersonalDevelopmentCustomer.getOldEmployeeId());
        personalMessage1.setReceiverName(handOverPersonalDevelopmentCustomer.getOldEmployeeId());

        personalMessageMapper.insert(personalMessage1);

    }

}
